import express from 'express';
import bodyParser from 'body-parser';
import createStrongErrorHandler from "strong-error-handler";
import path from "path";
import {sequelize} from './db';

export const app = express();

// 配置跨域
// app.all('*', function(req, res, next) {
//     res.header("Access-Control-Allow-Origin", "*");//允许所有来源访问
//     res.header("Access-Control-Allow-Headers", "*");
//     res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");//允许访问的方式
//     // res.header("X-Powered-By", ' 3.2.1')
//     // res.header("Content-Type", "application/json;charset=utf-8;multipart/form-data;");
//     next();
// })

app.all('*', function (req, res, next) {
    // 设置请求头为允许跨域
    res.header('Access-Control-Allow-Origin', '*');
    // 设置服务器支持的所有头信息字段
    res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild, sessionToken');
    // 设置服务器支持的所有跨域请求的方法
    res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
    if (req.method.toLowerCase() == 'options') {
        res.send(200);  // 让options尝试请求快速结束
    } else {
        next();
    }
});



// 日志
app.use(createStrongErrorHandler({
    debug: true
}))

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}))

app.use(express.static(path.join(__dirname, '../public')))

// 路由引入
import HomeRoute from './server/home'
import AnnounceRoute from './server/announce'
import DynamicRoute from './server/dynamic'
import likeRoute from './server/like'
import UserRoute from './server/user'
import actRegRouterFactory from './server/activeReg'
import activeRouterFactory from './server/acitve'
import CommentRouterFactory from './server/comment'
import UpFiles from './server/upfiles'
import weatherRegRouterFactory from './server/weather'

// 模型表引入
import {Swiper, Saying, Notice, Comment, Curriculum, Announce, Dynamic, Like, User, Admin,Active,ActiveReg} from "./models";

// 模型表注册
const swiperRepository = sequelize.getRepository(Swiper)
const curriculumRepository = sequelize.getRepository(Curriculum)
const noticeRepository = sequelize.getRepository(Notice)
const sayingRepository = sequelize.getRepository(Saying)
const announceRepository = sequelize.getRepository(Announce)
const dynamicRepository = sequelize.getRepository(Dynamic)
const likeRepository = sequelize.getRepository(Like)
const userRepository = sequelize.getRepository(User)
const adminRepository = sequelize.getRepository(Admin)
const commentRepository = sequelize.getRepository(Comment)
const activeRegRepository  = sequelize.getRepository(ActiveReg)
const activeRepository = sequelize.getRepository(Active)

// 路由注册
console.log(' --- 路由注册 ----');

app.use('/server/home', HomeRoute(swiperRepository, curriculumRepository, noticeRepository, sayingRepository))
app.use('/server/announce', AnnounceRoute(announceRepository))
app.use('/server/dynamic', DynamicRoute(dynamicRepository, likeRepository, commentRepository))
app.use('/server/like', likeRoute(likeRepository))
app.use('/server/user', UserRoute(userRepository, adminRepository))
app.use('/server/comment', CommentRouterFactory(commentRepository))
app.use('/server/actReg', actRegRouterFactory(activeRegRepository))
app.use('/server/active', activeRouterFactory(activeRepository))
app.use('/server', UpFiles())
app.use('/server', weatherRegRouterFactory())





